<?php

class sfToboAdsComponents extends sfComponents {

	public function executeAdBlock() {
		// retrieve the defaults
		$options = sfConfig::get("app_toboads_defaults");
		
		// extend the options provided by the caller
		$this->options = sfToolkit::arrayDeepMerge($options, $this->options);
		
		// prune any unexpected parameters
		$trash = array_diff(array_keys($this->options), array_keys($options));
		foreach($trash as $key)
			unset($this->options[$key]);
		
		unset($options);
		
		// validate the mandatory fields
		foreach(sfConfig::get('app_toboads_mandatory_fields') as $field) {
			if($this->options[$field] == "")
				throw new sfConfigurationException("sfToboAds ad block components called without mandatory field: $field");
		}
		
		// validate the fields
		$validation = sfConfig::get("app_toboads_validation");
		foreach(array_keys($validation) as $field) {
			$regex = $validation[$field];
			if(!preg_match($regex, $this->options[$field]))
				throw new sfConfigurationException("Parameter error: value of '$field' = '{$this->options[$field]}' does not match regex '$regex'");
		}
		
		$this->server_url = htmlspecialchars(sfConfig::get("app_toboads_relay_server"));
	}

}